package com.sqin.taskflow.listener;


import com.sqin.taskflow.mapper.JobResultMapper;
import com.sqin.taskflow.model.JobResult;
import com.sqin.taskflow.model.ProcessRecord;
import com.sqin.taskflow.service.ProcessRecordService;
import liquibase.pro.packaged.J;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.impl.event.FlowableEntityExceptionEventImpl;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.job.service.impl.persistence.entity.JobEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
*@Description: 全局任务监听
*@Param:
*@return:
*@Author: HO
*@date: 2020/5/15 15:09
*/
@Component
public class GlobalTaskListener extends AbstractFlowableEngineEventListener {
    protected static Logger logger = LoggerFactory.getLogger(GlobalTaskListener.class);

    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;

    @Resource
    private JobResultMapper jobResultMapper;

    @Autowired
    private ProcessRecordService processRecordService;

    @Override
    protected  void processCreated(FlowableEngineEntityEvent event){
        logger.info("process create");
    }

    @Override
    protected void processStarted(FlowableProcessStartedEvent event) {
        logger.info("process started");
    }

    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
        logger.info("task create");
    }
    @Override
    protected void processCompleted(FlowableEngineEntityEvent event){
        logger.info("process complete");
        String proc_ins_id = event.getProcessInstanceId();
        HistoricProcessInstance hiProcInst = historyService.createHistoricProcessInstanceQuery().processInstanceId(proc_ins_id).singleResult();
        String key = hiProcInst.getProcessDefinitionKey();
        ProcessRecord processRecordDB  = processRecordService.selectOneByProcId(key);
        processRecordDB.setStatus((byte) 2);
        processRecordService.updateById(processRecordDB);
    }
    @Override
    protected void taskCompleted(FlowableEngineEntityEvent event){
        logger.info("task complete");
    }

    @Override
    protected void jobExecutionSuccess(FlowableEngineEntityEvent event) {
        String proc_ins_id = event.getProcessInstanceId();
        HistoricProcessInstance hiProcInst = historyService.createHistoricProcessInstanceQuery().processInstanceId(proc_ins_id).singleResult();
        String key = hiProcInst.getProcessDefinitionKey();
        JobEntity jobEntity = (JobEntity)event.getEntity();
        JobResult jobResult = new JobResult();
        jobResult.setElementId(jobEntity.getElementId());
        jobResult.setProcId(key);
        jobResult.setProcInstId(proc_ins_id);
        jobResult.setStatus((byte)1);
        jobResult.setNote("成功");
        jobResult.setName(jobEntity.getElementName());
        jobResultMapper.insert(jobResult);
        logger.info("jobExecutionSuccess");
    }
    @Override
    protected void jobExecutionFailure(FlowableEngineEntityEvent event) {
        logger.info("jobExecutionSuccess");

        String proc_ins_id = event.getProcessInstanceId();
        HistoricProcessInstance hiProcInst = historyService.createHistoricProcessInstanceQuery().processInstanceId(proc_ins_id).singleResult();
        String key = hiProcInst.getProcessDefinitionKey();
        FlowableEntityExceptionEventImpl flowableEntityExceptionEvent = (FlowableEntityExceptionEventImpl) event;
        JobEntity jobEntity = (JobEntity)event.getEntity();
        JobResult jobResult = new JobResult();
        jobResult.setElementId(jobEntity.getElementId());
        jobResult.setProcId(key);
        jobResult.setProcInstId(proc_ins_id);
        jobResult.setStatus((byte)2);
        jobResult.setNote(flowableEntityExceptionEvent.getCause().getMessage());
        jobResult.setName(jobEntity.getElementName());
        jobResultMapper.insert(jobResult);

        ProcessRecord processRecordDB  = processRecordService.selectOneByProcId(key);
        processRecordDB.setStatus((byte) 3);
        processRecordService.updateById(processRecordDB);
    }
}
